package com.ziyueyuwen.xiniao.util;

import com.suyeer.basic.util.LogUtil;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author jun 2018/12/4
 */
public class CacheUtil {

    private final static ConcurrentHashMap<String, Long> CACHE_TIME = new ConcurrentHashMap<>();

    /**
     * User: jun
     * Date: 2017/8/7
     * Desc: 设置MC的标记时间
     */
    public static void setNextUpdateTimeToMem(String key) {
        try {
            CACHE_TIME.put(key, System.currentTimeMillis());
        } catch (Exception e) {
            LogUtil.error(e.getMessage());
        }
    }

    /**
     * User: jun
     * Date: 2017/8/7
     * Desc: 判断缓存是否更新
     */
    public static Boolean isNeedInit(String key, long lastTime, boolean isEmpty) {
        boolean flag = true;
        try {
            Long mcTime = CACHE_TIME.get(key);
            if (mcTime != null && !isEmpty) {
                flag = mcTime > lastTime;
            }
            if (mcTime == null) {
                setNextUpdateTimeToMem(key);
            }
        } catch (Exception e) {
            LogUtil.error("读取MC失败, 默认需要更新: {}!", e.getMessage());
        } return flag;
    }
}
